
package com.yyfax.ph.personal;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

//todo 分享链接单独放到一个presenter中
public class PersonCenterModel extends Model {
	private String TAG = PersonCenterModel.class.getSimpleName();

	public static final int MSG_CHECK_BALANCE = 405;

	public static final int MSG_GET_APPLY_COUNT = 53;
	public static final int MSG_GET_MSG_COUNT = 55;

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		switch (functionalCode) {
			case MSG_GET_APPLY_COUNT:
				return service.getApplyCount(token, new EmptyEntity());
			case MSG_CHECK_BALANCE:
				return service.checkBalance(token, new EmptyEntity());
			case MSG_GET_MSG_COUNT:
				return service.getMsgCount(token, new EmptyEntity());
		}
		return null;
	}

	@Override
	public void onNext(Object obj) {
		BaseResultEntity result = cast(obj);
		CLog.i(TAG, "onNext result = " + result.toString());
		switch (result.requestCode) {
			case MSG_GET_APPLY_COUNT:
				ApplyCountResult count = cast(obj);
				EventAgent.post(count);
				break;
			case MSG_CHECK_BALANCE:
				BalanceResult balance = cast(obj);
				EventAgent.post(balance);
				break;
			case MSG_GET_MSG_COUNT:
				MsgResult msg = cast(obj);
				EventAgent.post(msg);
				break;
		}
	}

	public interface Service {

		@POST("account/bind/getApplyCount")
		Observable<ApplyCountResult> getApplyCount(@Header("token") String token, @Body EmptyEntity params);

		@POST("account/balance/query")
		Observable<BalanceResult> checkBalance(@Header("token") String token, @Body EmptyEntity params);

		@POST("sys/letter/count")
		Observable<MsgResult> getMsgCount(@Header("token") String token, @Body EmptyEntity emptyEntity);
	}

	public static class MsgResult extends BaseResultEntity<MsgBean> {
	}

	public static class MsgBean {
		public int count;
	}



	static class CountBean {
		String count;
	}

	public static class ApplyCountResult extends BaseResultEntity<CountBean> {

	}

	public static class BalanceBean {
		public String alreadywithdraw;
		public String staywithdraw;
		public String bank;
		public String cardId;
	}

	public static class BalanceResult extends BaseResultEntity<BalanceBean> {

	}
}
